package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import dao.TeamDao;
import dto.TeamDto;

public class TeamDaoImpl extends SimpleJdbcTemplate implements TeamDao {

	public TeamDaoImpl(DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public List getTeamList() throws DataAccessException {
		RowMapper row = new TeamRowMapper();
		return query("select team_id, name from team", row);
	}
	
	protected class TeamRowMapper implements RowMapper{
		private List teamList = new ArrayList();
		
		public List getResults(){
			return teamList;
		}
		
		@Override
		public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
			TeamDto team = new TeamDto();
			team.setId(arg0.getInt("team_id"));
			team.setName(arg0.getString("name"));
			return team;
		}
	}

	@Override
	public TeamDto getTeam(Integer id) throws DataAccessException {
		RowMapper row = new TeamRowMapper();
		return queryForObject("select team_id, name from team where team_id=?", row, id);
	}

}
